package com.aidong.web.controller.dic;

/**
 * Created by john on 16-11-3.
 */

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.web.controller.BaseController;

import java.util.Date;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"dicShop"})
public class DicShopController extends BaseController {
    @Resource
    DicShopCodeDao dicShopCodeDao;

    public DicShopController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("dic/shop/list")).addObject("areaType", this.getTypeList("areaType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "shopType", required = false) String shopType, @RequestParam(value = "areaType", required = false) String areaType, @RequestParam(value = "pageNo", required = false) String pageNo, @RequestParam(value = "totalCount", required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.gt("status", Integer.valueOf(0));
        cd.eq("shopType", shopType);
        cd.eq("areaType", areaType);
        if (!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("code", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.dicShopCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv1 = new ModelAndView("dic/shop/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        DicShopCode shop = new DicShopCode();
        shop.setStatus(Integer.valueOf(1));
        shop.setMakeCard(Integer.valueOf(1));
        shop.setNeedPaperContract(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("dic/shop/edit");
        mv.addObject("shop", shop);
        mv.addObject("shopType", this.getTypeList("shopType"));
        mv.addObject("areaType", this.getTypeList("areaType"));
        mv.addObject("shopLevel", this.getTypeList("shopLevel"));
        mv.addObject("shopNameSeal", this.getTypeList("shopNameSeal"));
        mv.addObject("contractStyle", this.getTypeList("contractStyle"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("shop") DicShopCode shop) {
        if (this.chkNull(shop.getCode())) {
            return this.getResult(false, "请输入门店编码");
        } else if (this.chkNull(shop.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if (this.chkNull(shop.getShopType())) {
            return this.getResult(false, "请选择类型");
        } else {
            if (this.chkNull(shop.getStatus())) {
                shop.setStatus(Integer.valueOf(1));
            }

            if (this.chkNull(shop.getMakeCard())) {
                shop.setMakeCard(Integer.valueOf(0));
            }

            if (this.chkNull(shop.getNeedPaperContract())) {
                shop.setNeedPaperContract(Integer.valueOf(0));
            }

            if (this.dicShopCodeDao.exists(shop.getCode())) {
                return this.getResult(false, "门店编码已存在");
            } else {
                shop.setCreateUser(this.getLoginUserCode());
                shop.setCreateTime(new Date());
                shop.setUpdateTime(new Date());

                try {
                    this.dicShopCodeDao.add(shop);
                    this.reloadTypeList("shopName");
                    this.addUserLog("新增门店", "编号:" + shop.getCode() + ",名称:" + shop.getChName());
                } catch (Exception var3) {
                    this.log.error("add dicshop", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        DicShopCode shop = (DicShopCode) this.dicShopCodeDao.getById(code);
        if (shop == null) {
            return this.showAlert("没有这个门店");
        } else {
            ModelAndView mv = new ModelAndView("dic/shop/edit");
            mv.addObject("shop", shop);
            mv.addObject("shopType", this.getTypeList("shopType"));
            mv.addObject("areaType", this.getTypeList("areaType"));
            mv.addObject("shopLevel", this.getTypeList("shopLevel"));
            mv.addObject("shopNameSeal", this.getTypeList("shopNameSeal"));
            mv.addObject("contractStyle", this.getTypeList("contractStyle"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("shop") DicShopCode shop) {
        if (this.chkNull(shop.getCode())) {
            return this.getResult(false, "没有这个门店");
        } else if (this.chkNull(shop.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if (this.chkNull(shop.getShopType())) {
            return this.getResult(false, "请选择类型");
        } else {
            if (this.chkNull(shop.getStatus())) {
                shop.setStatus(Integer.valueOf(1));
            }

            if (this.chkNull(shop.getMakeCard())) {
                shop.setMakeCard(Integer.valueOf(0));
            }

            if (this.chkNull(shop.getNeedPaperContract())) {
                shop.setNeedPaperContract(Integer.valueOf(0));
            }

            DicShopCode shop1 = (DicShopCode) this.dicShopCodeDao.getById(shop.getCode());
            if (shop1 == null) {
                return this.getResult(false, "没有这个门店");
            } else {
                shop1.setChName(shop.getChName());
                shop1.setShopType(shop.getShopType());
                shop1.setLevelCode(shop.getLevelCode());
                shop1.setAreaType(shop.getAreaType());
                shop1.setAddress(shop.getAddress());
                shop1.setPhone(shop.getPhone());
                shop1.setLinkman(shop.getLinkman());
                shop1.setFax(shop.getFax());
                shop1.setStatus(shop.getStatus());
                shop1.setSort(shop.getSort());
                shop1.setMakeCard(shop.getMakeCard());
                shop1.setNeedPaperContract(shop.getNeedPaperContract());
                shop1.setCompanyName(shop.getCompanyName());
                shop1.setSealPic(shop.getSealPic());
                shop1.setContractStyle(shop.getContractStyle());
                shop1.setUpdateTime(new Date());

                try {
                    this.dicShopCodeDao.update(shop1);
                    this.reloadTypeList("shopName");
                    this.addUserLog("修改门店", "编号:" + shop1.getCode() + ",名称:" + shop1.getChName());
                } catch (Exception var4) {
                    this.log.error("edit dicshop", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        DicShopCode shop = (DicShopCode) this.dicShopCodeDao.getById(code);
        if (shop == null) {
            return this.getResult(false, "没有这个门店");
        } else {
            try {
                this.dicShopCodeDao.updateStatus(code, 0);
                this.reloadTypeList("shopName");
                this.addUserLog("删除门店", "编号:" + shop.getCode() + ",名称:" + shop.getChName());
            } catch (Exception var4) {
                this.log.error("del dicshop", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
